package com.version.manager.service;

import com.version.manager.entity.Menu;
import com.version.manager.entity.Role;
import com.version.manager.entity.User;
import com.version.manager.util.JWTUtil;
import com.version.manager.vo.FrontUser;
import com.version.manager.vo.JwtAuthenticationRequest;
import com.version.manager.vo.PermissionInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import com.version.manager.constant.AdminCommonConstant;

@Service
@Transactional(rollbackFor = Exception.class)
public class PermissionService {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    public List<PermissionInfo> getMenuPermission(Integer userId){
        //用户可能有多个角色
        List<Role> roles=roleService.getRoleByUser(userId);
        if(roles==null){
            return null;
        }

        List<Menu> menus=new ArrayList<>();
        List<Integer> ids=new ArrayList<>();
        //遍历角色，获取菜单权限
        for(Role ro:roles){
            String roleId=ro.getId();
            List<Menu> m=menuService.getMenuByRole(roleId);
            for(Menu it:m){
                if(ids.contains(it.getId())){
                    continue;
                }
                menus.add(it);
                ids.add(it.getId());
            }
        }
        //遍历角色，获取按钮权限

        //转为Permission
        List<PermissionInfo> permissionInfos=new ArrayList<>();
        for(Menu m:menus){
            permissionInfos.add(menu2PermissionInfo(m));
        }
        return permissionInfos;

    }

    private PermissionInfo menu2PermissionInfo(Menu menu){
        PermissionInfo info=new PermissionInfo();
        info.setCode(menu.getCode());
        info.setMenu(menu.getTitle());
        info.setType(AdminCommonConstant.RESOURCE_TYPE_MENU);
        info.setName(AdminCommonConstant.RESOURCE_ACTION_VISIT);
        info.setMethod(AdminCommonConstant.RESOURCE_REQUEST_METHOD_GET);
        String uri=menu.getHref();
        if (!uri.startsWith("/")) {
            uri = "/" + uri;
        }
        info.setUri(uri);
        return info;
    }

    public FrontUser getUserInfo(String token){
        JwtAuthenticationRequest jwt=JWTUtil.getJwtInfo(token);
        if(jwt==null||jwt.getCompanyCode()==null||jwt.getUserName()==null){
            return null;
        }
        FrontUser frontUser=new FrontUser();
        User info=userService.getUserByCompanyCode(jwt.getCompanyCode(),jwt.getUserName());
        //BeanUtils.copyProperties(frontUser, info);
        frontUser.setId(info.getId());
        frontUser.setUserName(info.getName());
        List<PermissionInfo> menuPermission=getMenuPermission(info.getId());
        frontUser.setMenus(menuPermission);

        List<PermissionInfo> elements=new ArrayList<>();
        frontUser.setElements(elements);
        return frontUser;
    }
}
